package org.jground.zk.lab;

import java.util.Comparator;

import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import org.jground.rx.ui.zk.UIStyles;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.AbstractListModel;
import org.zkoss.zul.Caption;
import org.zkoss.zul.ListModelExt;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listhead;
import org.zkoss.zul.Listheader;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Window;

public class ListboxWindow extends Window implements EventListener {

    private static Log logger = LogFactory.getLog(ListboxWindow.class);

    public ListboxWindow() {
        super("Listbox Window", "normal", true);
        Caption caption = new Caption();
        caption.setImage("/img/other22.png");
        caption.setParent(this);
        createUI();
    }

    public void createUI() {
        Listbox listbox = new Listbox();
        listbox.setParent(this);

        listbox.addEventListener(Events.ON_SELECT, this);
        //
        Listhead head = new Listhead();
        head.setSizable(true);
        head.setParent(listbox);
        //
        Listheader lhr = new Listheader();
        lhr.setWidth("2%");
        lhr.setSort("auto");
        lhr.setParent(head);
        //
        lhr = new Listheader("");
        lhr.setParent(head);
        //
        lhr = new Listheader("");
        lhr.setParent(head);
        //
        lhr = new Listheader("");
        lhr.setWidth("2%");
        lhr.setParent(head);
        //
        // lhr = new Listheader("");
        // lhr.setWidth("2%");
        // lhr.setParent(head);
        //
        listbox.setModel(new MyListModel());

        listbox.setItemRenderer(new MyRenderer());
    }

    public void onEvent(Event e) throws Exception {
        if (e.getTarget() instanceof Listcell) {
            Listcell c = (Listcell) e.getTarget();
            logger.info("selected listcell" + e.getTarget() + " with CMD " + c.getAttribute("CMD"));
        }

    }

    private class MyListModel extends AbstractListModel implements ListModelExt {

        String[][] data = { { "hans", "müller" }, { "sepp", "meier" }, { "fritz", "huber" }, { "franz", "herger" },
                { "anton", "schuler" } };

        public Object getElementAt(int index) {
            return data[index];
        }

        public int getSize() {
            // TODO Auto-generated method stub
            return 5;
        }

        @SuppressWarnings("unchecked")
        public void sort(Comparator cmpr, boolean ascending) {
            // TODO Auto-generated method stub
            if (ascending) {

            }
        }
    }

    private class MyRenderer implements ListitemRenderer {

        public void render(Listitem item, Object data) throws Exception {
            // ICON
            Listcell c = new Listcell();
            c.setImage("/img/other22.png");
            c.setParent(item);
            c.addEventListener(Events.ON_CLICK, ListboxWindow.this);
            //
            c = new Listcell("c1");
            c.setValue("c1");
            c.setParent(item);
            c.addEventListener(Events.ON_CLICK, ListboxWindow.this);
            //
            c = new Listcell("c2222");
            c.setParent(item);
            c.addEventListener(Events.ON_CLICK, ListboxWindow.this);
            //
            c = new Listcell();
            c.setImage("/img/view16.png");
            c.setValue("view");
            c.setParent(item);
            c.addEventListener(Events.ON_CLICK, ListboxWindow.this);
            //
            c = new Listcell();
            c.setAttribute("CMD", "EDIT");
            c.setImageContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.edit16));
            c.setValue("edit");
            c.setParent(item);
            c.addEventListener(Events.ON_CLICK, ListboxWindow.this);
            c.setTooltiptext("Edit current object");
            //
        }
    }
}
